try:
  import usocket as socket
except:
  import socket
from machine import Pin

led_red = Pin(13, Pin.OUT)
led_yellow = Pin(12,Pin.OUT)

def web_page():
    if led_red.value() == 0:
        gpio_red_state = 'off'
    elif led_red.value() == 1:
        gpio_red_state = 'on'
    if led_yellow.value() == 0:
        gpio_yellow_state = 'off'
    elif led_yellow.value() == 1:
        gpio_yellow_state = 'on'
        
    # html code
    html ='''
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ESP32</title>
    <h1 >led_red</h1>
    <p>GPIO state:
        <strong>
             '''+ gpio_red_state +''' 
        </strong>
    </p>
    <p>
        <span >开</span>
        <a href="/?led_red=on"><button class="button">ON</button>
        </a>
    </p>
    <p>
        <span >关</span>
        <a href="/?led_red=off"><button class="button button2">OFF</button>
        </a>
    </p>
    <h1 >led_yellow</h1>
    <p>GPIO state:
        <strong>
             '''+ gpio_yellow_state +''' 
        </strong>
    </p>
    <p>
        <span >开</span>
        <a href="/?led_yellow=on"><button class="button">ON</button>
        </a>
    </p>
    <p>
        <span >关</span>
        <a href="/?led_yellow=off"><button class="button button2">OFF</button>
        </a>
    </p>
</head>
<body>
'''
    return html

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)

while True:
    conn, addr = s.accept()
    print('Connection: %s' % str(addr))
    req = conn.recv(1024)
    req = str(req)
    print('Connect = %s' % req)
    led_red_on = req.find('/?led_red=on')
    led_red_off = req.find('/?led_red=off')
    led_yellow_on = req.find('/?led_yellow=on')
    led_yellow_off = req.find('/?led_yellow=off')
    if led_red_on == 6:
        print('led_red on')
        led_red.value(1)
    elif led_red_off == 6:
        print('led_red off')
        led_red.value(0)
    if led_yellow_on == 6:
        print('led_yellow on')
        led_yellow.value(1)
    elif led_yellow_off == 6:
        print('led_yellow off')
        led_yellow.value(0)
    response = web_page()
    conn.send('HTTP/1.1 200 OK\n')
    conn.send('Content-Type: text/html\n')
    conn.send('Connection: close\n\n')
    conn.sendall(response)
    conn.close()